﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace cjwygl
{
    public partial class FrmPrint : Form
    {
        private string mMonth;
        private int mIsPaid;
        public FrmPrint(String month, int isPaid)
        {
            InitializeComponent();
            mMonth = month;
            mIsPaid = isPaid;
        }

        private void FrmPrint_Load(object sender, EventArgs e)
        {
            CrystalReport_Mainfee rMainfee = new CrystalReport_Mainfee();
            List<CMainfeeNotice> printList = new List<CMainfeeNotice>();
            DataTable dtReport;
            using (DataBase db = new DataBase())
            {
                String sql = "SELECT * FROM View_MAIN_FEE_REPORT WHERE 1=1 ";

                if (mMonth != "<全部>")
                {
                    sql += " AND GEN_MONTH=@GEN_MONTH ";
                    db.AddParameter("GEN_MONTH", mMonth);
                }
                if (mIsPaid == 1)
                {
                    sql += " AND IS_PAID=1 ";
                }
                else if (mIsPaid == 2)
                {
                    sql += " AND IS_PAID=0 ";
                }

                dtReport = db.ExecuteDataTable(sql);
            }

            if (dtReport.Rows.Count == 0)
            {
                MessageBox.Show("没有需要打印的记录");
                Dispose();
                return;
            }

            foreach (DataRow dr in dtReport.Rows)
            {
                float subtotal = 0;
                CMainfeeNotice notice = new CMainfeeNotice();

                notice.customer = dr["COMPANY_NAME"].ToString();
                notice.roomNo = dr["ROOM_NO"].ToString();
                notice.noticeDate = ((DateTime)dr["FZ_LE"]).ToString("yyyy.M.d");

                float dFWF = 0;
                if (dr["FWF_FEE"].ToString() != "")
                {
                    dFWF = (float)dr["FWF_FEE"];
                }
                subtotal = (float)dr["FZ_FEE"] + dFWF;

                notice.feeType1 = "房租";
                notice.feeDate1 = ((DateTime)dr["FZ_LE"]).ToString("yy.M.d") + "－" + ((DateTime)dr["FZ_NS"]).AddDays(-1).ToString("yy.M.d");
                notice.feeAmount1 = subtotal.ToString();
                notice.comment1 = dr["FZ_COMMENT"].ToString() + "  " + dr["FWF_COMMENT"].ToString();

                if (dr["WY_FEE"].ToString() != "")
                {
                    notice.feeType2 = "物业费";
                    notice.feeDate2 = ((DateTime)dr["WY_LE"]).ToString("yy.M.d") + "－" + ((DateTime)dr["WY_NS"]).AddDays(-1).ToString("yy.M.d");
                    notice.feeAmount2 = dr["WY_FEE"].ToString();
                    notice.comment2 = dr["WY_COMMENT"].ToString();
                    subtotal += (float)dr["WY_FEE"];
                }

                if (dr["WL_FEE"].ToString() != "" && dr["FY_FEE"].ToString() != "")
                {
                    notice.feeType3 = "网络费、复印";
                    notice.feeDate3 = ((DateTime)dr["WL_LE"]).ToString("yy.M.d") + "－" + ((DateTime)dr["WL_NS"]).AddDays(-1).ToString("yy.M.d");
                    notice.feeAmount3 = ((float)dr["WL_FEE"] + (float)dr["FY_FEE"]).ToString();
                    notice.comment3 = dr["WL_COMMENT"].ToString() + "  " + dr["FY_COMMENT"].ToString();
                    subtotal += (float)dr["WL_FEE"] + (float)dr["FY_FEE"];
                }
                else if (dr["WL_FEE"].ToString() != "")
                {
                    notice.feeType3 = "网络费";
                    notice.feeDate3 = ((DateTime)dr["WL_LE"]).ToString("yy.M.d") + "－" + ((DateTime)dr["WL_NS"]).AddDays(-1).ToString("yy.M.d");
                    notice.feeAmount3 = dr["WL_FEE"].ToString();
                    notice.comment3 = dr["WL_COMMENT"].ToString();
                    subtotal += (float)dr["WL_FEE"];
                }
                else if (dr["FY_FEE"].ToString() != "")
                {
                    notice.feeType3 = "复印";
                    notice.feeDate3 = ((DateTime)dr["FY_LE"]).ToString("yy.M.d") + "－" + ((DateTime)dr["FY_NS"]).AddDays(-1).ToString("yy.M.d");
                    notice.feeAmount3 = dr["FY_FEE"].ToString();
                    notice.comment3 = dr["FY_COMMENT"].ToString();
                    subtotal += (float)dr["FY_FEE"];
                }

                if (dr["CW_FEE"].ToString() != "")
                {
                    notice.feeType4 = "车位费";
                    notice.feeDate4 = ((DateTime)dr["CW_LE"]).ToString("yy.M.d") + "－" + ((DateTime)dr["CW_NS"]).AddDays(-1).ToString("yy.M.d");
                    notice.feeAmount4 = dr["CW_FEE"].ToString();
                    notice.comment4 = dr["CW_COMMENT"].ToString();
                    subtotal += (float)dr["CW_FEE"];
                }

                notice.subtotal = subtotal.ToString();

                if (dr["SF_FEE"].ToString() != "" && dr["DF_FEE"].ToString() != "")
                {
                    notice.feeType5 = "水费、电费";
                    notice.feeDate5 = ((DateTime)dr["SF_LE"]).ToString("yy.M.d") + "(抄表)";
                    notice.feeAmount5 = ((float)dr["SF_FEE"] + (float)dr["DF_FEE"]).ToString();
                    notice.comment5 = dr["SF_COMMENT"].ToString() + "  " + dr["DF_COMMENT"].ToString();
                    subtotal += (float)dr["SF_FEE"] + (float)dr["DF_FEE"];
                }
                else if (dr["SF_FEE"].ToString() != "")
                {
                    notice.feeType5 = "水费";
                    notice.feeDate5 = ((DateTime)dr["SF_LE"]).ToString("yy.M.d") + "(抄表)";
                    notice.feeAmount5 = dr["SF_FEE"].ToString();
                    notice.comment5 = dr["SF_COMMENT"].ToString();
                    subtotal += (float)dr["SF_FEE"];
                }
                else if (dr["DF_FEE"].ToString() != "")
                {
                    notice.feeType5 = "电费";
                    notice.feeDate5 = ((DateTime)dr["DF_LE"]).ToString("yy.M.d") + "(抄表)";
                    notice.feeAmount5 = dr["DF_FEE"].ToString();
                    notice.comment5 = dr["DF_COMMENT"].ToString();
                    subtotal += (float)dr["DF_FEE"];
                }

                notice.total = subtotal.ToString();

                printList.Add(notice);
            }

            rMainfee.SetDataSource(printList);
            crystalReportViewer1.ReportSource = rMainfee;
        }
    }
}
